package com.casstime.ec.cloud.meeting.service.handler;

import com.casstime.commons.service.exception.HttpMessageException;
import com.casstime.ec.cloud.meeting.dao.entity.EntityBook;
import com.casstime.ec.cloud.meeting.dao.entity.EntityBookDetail;
import com.casstime.ec.cloud.meeting.dao.entity.EntityDesc;
import com.casstime.ec.cloud.meeting.dao.entity.EntityRoom;
import com.casstime.ec.cloud.meeting.dao.entity.EntityUser;
import com.casstime.ec.cloud.meeting.dao.entity.example.EntityBookExample;
import com.casstime.ec.cloud.meeting.dao.mapper.BookRoomMapper;
import com.casstime.ec.cloud.meeting.dao.mapper.EntityBookMapper;
import com.casstime.ec.cloud.meeting.dao.mapper.EntityDescMapper;
import com.casstime.ec.cloud.meeting.dao.mapper.EntityRoomMapper;
import com.casstime.ec.cloud.meeting.dao.mapper.EntityUserMapper;
import com.casstime.ec.cloud.meeting.model.BookDetail;
import com.casstime.ec.cloud.meeting.values.BookDetailQuery;
import com.casstime.ec.cloud.meeting.values.BookMeetingRoom;
import com.casstime.ec.cloud.meeting.values.BookMeetingRoomParam;
import com.casstime.ec.cloud.meeting.values.DeleteMyBook;
import com.casstime.ec.cloud.meeting.values.ResultBookMeetingRoom;
import com.casstime.ec.cloud.meeting.values.ResultDeleteMyBook;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 预约单详情处理类
 */
@Service
public class BookDeatilHandler {

  @Autowired
  private BookRoomMapper bookRoomMapper;

  @Autowired
  private EntityBookMapper entityBookMapper;

  @Autowired
  private EntityUserMapper EntityUserMapper;

  @Autowired
  private EntityRoomMapper entityRoomMapper;

  @Autowired
  private EntityDescMapper entityDescMapper;


  public BookDetail getBookDetailById(Integer bookDetailId) {
    EntityBookDetail entityBookDetail = bookRoomMapper.selectBookDetailById(bookDetailId);
    return BookDetail.get(entityBookDetail);
  }


  public List<BookDetail> getBookDetailList() {
    List<BookDetail> bookDetailList = new ArrayList<BookDetail>();
    EntityBookExample entityBookExample = new EntityBookExample();
    entityBookExample.createCriteria();
    List<EntityBook> list = entityBookMapper.selectByExample(entityBookExample);
    for (EntityBook entityBook : list) {
      EntityRoom entityRoom = entityRoomMapper.selectByPrimaryKey(entityBook.getRoomId());
      EntityUser entityUser = EntityUserMapper.selectByPrimaryKey(entityBook.getApplyUser());
      BookDetail bookDetail = new BookDetail();
      bookDetail.setId(entityBook.getId());
      bookDetail.setRoomName(entityRoom.getRoomName());
      bookDetail.setTheme(entityBook.getTheme());
      bookDetail.setApplyUserName(entityUser.getUsername());
      bookDetail.setStartAt(entityBook.getBeginAt());
      bookDetail.setEndAt(entityBook.getEndAt());
      bookDetailList.add(bookDetail);
    }
    return bookDetailList;
  }


  public List<BookDetail> getBookDetailByQuery(BookDetailQuery bookDetailQuery) {
    List<BookDetail> bookDetailList = new ArrayList<BookDetail>();
    List<EntityBook> entityBookList = entityBookMapper.getBookDetailByQuery(bookDetailQuery);
    if (entityBookList.size() == 0) {
      return null;
    } else {
      for (EntityBook entityBook : entityBookList) {
        EntityRoom entityRoom = entityRoomMapper.selectByPrimaryKey(entityBook.getRoomId());
        EntityUser entityUser = EntityUserMapper.selectByPrimaryKey(entityBook.getApplyUser());
        BookDetail bookDetail = new BookDetail();
        bookDetail.setId(entityBook.getId());
        bookDetail.setRoomName(entityRoom.getRoomName());
        bookDetail.setTheme(entityBook.getTheme());
        bookDetail.setApplyUserName(entityUser.getUsername());
        bookDetail.setEndAt(entityBook.getEndAt());
        bookDetail.setStartAt(entityBook.getBeginAt());
        bookDetailList.add(bookDetail);
      }
      return bookDetailList;
    }
  }


  public List<BookDetail> getBookDetailByUserId(Integer userId) {
    EntityBookExample entityBookExample = new EntityBookExample();
    entityBookExample.createCriteria().andApplyUserEqualTo(userId);
    List<EntityBook> entityBookList = entityBookMapper.selectByExample(entityBookExample);
    List<BookDetail> bookDetailList = new ArrayList<BookDetail>();
    if (entityBookList.size() == 0) {
      return null;
    } else {
      for (EntityBook entityBook : entityBookList) {
        EntityRoom entityRoom = entityRoomMapper.selectByPrimaryKey(entityBook.getRoomId());
        EntityUser entityUser = EntityUserMapper.selectByPrimaryKey(entityBook.getApplyUser());
        BookDetail bookDetail = new BookDetail();
        bookDetail.setId(entityBook.getId());
        bookDetail.setTheme(entityBook.getTheme());
        bookDetail.setRoomName(entityRoom.getRoomName());
        bookDetail.setApplyUserName(entityUser.getUsername());
        bookDetail.setStartAt(entityBook.getBeginAt());
        bookDetail.setEndAt(entityBook.getEndAt());
        bookDetailList.add(bookDetail);
      }
      return bookDetailList;
    }
  }


  /**
   * 预约会议室
   */
  public ResultBookMeetingRoom bookMeetingRoom(BookMeetingRoomParam bookMeetingRoomParam)
      throws ParseException {
    ResultBookMeetingRoom resultBookMeetingRoom = new ResultBookMeetingRoom();
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    ParsePosition pos = new ParsePosition(8);

    Date begin = format.parse(bookMeetingRoomParam.getStartAt());
    Date end = format.parse(bookMeetingRoomParam.getEndAt());
    if (bookMeetingRoomParam == null) {
      throw new HttpMessageException(417, "参数不能为空");
    }
    List<EntityBook> entityBookList = entityBookMapper.selectCanBook(bookMeetingRoomParam);
    if (entityBookList.size() > 0) {
      resultBookMeetingRoom.setCode(400);
      resultBookMeetingRoom.setMsg("当前时间段已被预约，请重新预约");
      resultBookMeetingRoom.setData(null);
    } else {

      EntityBook entityBook = EntityBook.builder()
          .roomId(bookMeetingRoomParam.getRoomId())
          .applyUser(bookMeetingRoomParam.getApplyUserId())
          .theme(bookMeetingRoomParam.getTheme())
          .beginAt(begin)
          .endAt(end).build();
      entityBookMapper.insertSelective(entityBook);
      EntityDesc entityDesc = new EntityDesc();
      entityDesc.setBookId(entityBook.getId());
      entityDesc.setDesc(bookMeetingRoomParam.getDesc());
      entityDescMapper.insert(entityDesc);

      BookMeetingRoom bookMeetingRoom = new BookMeetingRoom();
      bookMeetingRoom.setRoomId(bookMeetingRoomParam.getRoomId());
      bookMeetingRoom.setApplyUserId(bookMeetingRoomParam.getApplyUserId());
      bookMeetingRoom.setTheme(bookMeetingRoomParam.getTheme());
      bookMeetingRoom.setStartAt(bookMeetingRoomParam.getStartAt());
      bookMeetingRoom.setEndAt(bookMeetingRoomParam.getEndAt());
      bookMeetingRoom.setDesc(bookMeetingRoomParam.getDesc());

      resultBookMeetingRoom.setCode(200);
      resultBookMeetingRoom.setMsg("预约成功");
      resultBookMeetingRoom.setData(bookMeetingRoom);


    }

    return resultBookMeetingRoom;


  }




  public ResultDeleteMyBook deleteMyBook(Integer bookId) {

      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

      if(bookId == null) {
        throw new HttpMessageException(417, "参数不能为空");
      }
      ResultDeleteMyBook resultDeleteMyBook = new ResultDeleteMyBook();
      DeleteMyBook deleteMyBook = new DeleteMyBook();

      EntityBook entityBook = entityBookMapper.selectByPrimaryKey(bookId);
      int resultBook = entityBookMapper.deleteByPrimaryKey(bookId);
      if(resultBook > 0) {
         resultDeleteMyBook.setCode(200);
         resultDeleteMyBook.setMsg("删除成功");

         deleteMyBook.setId(entityBook.getId());
         deleteMyBook.setRoomId(entityBook.getRoomId());
         deleteMyBook.setApplyUserId(entityBook.getApplyUser());
         deleteMyBook.setTheme(entityBook.getTheme());
         deleteMyBook.setStartAt(format.format(entityBook.getBeginAt()));
         deleteMyBook.setEndAt(format.format(entityBook.getEndAt()));

         EntityDesc entityDesc = entityDescMapper.selectDescByBookId(bookId);
         int resultDesc = entityDescMapper.deleteDescByBookId(bookId);
         if(resultDesc > 0) {
           deleteMyBook.setDesc(entityDesc.getDesc());
         }

         resultDeleteMyBook.setData(deleteMyBook);


      } else {
        resultDeleteMyBook.setCode(400);
        resultDeleteMyBook.setMsg("删除失败");
        resultDeleteMyBook.setData(null);
      }

       return resultDeleteMyBook;

  }







}
